﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.Object;
using Model.DataContext;

namespace Nongsan.Controllers
{
    public class ChitiethoadonController : Controller
    {
        private DataContext db = new DataContext();

        //abc
        // GET: /Chitiethoadon/
       public ActionResult jsonList(int idhoadon)
        {
            var list = from n in db.ChiTietHoaDons where n.ID_Hoadon==idhoadon select new {n.SanPham.Ten,n.SanPham.HinhAnhs.FirstOrDefault().ImagePath,n.SanPham.Gia,n.So_luong,n.Thanh_tien,n.ID };	
            return Json(list, JsonRequestBehavior.AllowGet);
        } 
		
        public ActionResult Index(int idhoadon)
        {
            var hd = (from n in db.HoaDons where n.ID == idhoadon select n).FirstOrDefault();
            ViewBag.idhoadon = idhoadon;
            ViewBag.hd = hd;
            return View();

        }

        //
        // GET: /Chitiethoadon/Details/5

        public ActionResult Details(int id = 0)
        {
            ChiTietHoaDon chitiethoadon = db.ChiTietHoaDons.Find(id);
            if (chitiethoadon == null)
            {
                return HttpNotFound();
            }
            return View(chitiethoadon);
        }

        //
        // GET: /Chitiethoadon/Create

        public ActionResult Create()
        {
            ViewBag.ID_Sanpham = new SelectList(db.SanPhams, "ID", "Ten");
            ViewBag.ID_Hoadon = new SelectList(db.HoaDons, "ID", "Dia_chi_nhan");
            return View();
        }

        //
        // POST: /Chitiethoadon/Create
		   [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(ChiTietHoaDon chitiethoadon)
        {
            if (ModelState.IsValid)
            {
                db.ChiTietHoaDons.Add(chitiethoadon);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ID_Sanpham = new SelectList(db.SanPhams, "ID", "Ten", chitiethoadon.ID_Sanpham);
            ViewBag.ID_Hoadon = new SelectList(db.HoaDons, "ID", "Dia_chi_nhan", chitiethoadon.ID_Hoadon);
            return View(chitiethoadon);
        }

        //
        // GET: /Chitiethoadon/Edit/5

        public ActionResult Edit(int id = 0)
        {
            ChiTietHoaDon chitiethoadon = db.ChiTietHoaDons.Find(id);
            if (chitiethoadon == null)
            {
                return HttpNotFound();
            }
            ViewBag.ID_Sanpham = new SelectList(db.SanPhams, "ID", "Ten", chitiethoadon.ID_Sanpham);
            ViewBag.ID_Hoadon = new SelectList(db.HoaDons, "ID", "Dia_chi_nhan", chitiethoadon.ID_Hoadon);
            return View(chitiethoadon);
        }

        //
        // POST: /Chitiethoadon/Edit/5
		   [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(ChiTietHoaDon chitiethoadon)
        {
            if (ModelState.IsValid)
            {
                db.Entry(chitiethoadon).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ID_Sanpham = new SelectList(db.SanPhams, "ID", "Ten", chitiethoadon.ID_Sanpham);
            ViewBag.ID_Hoadon = new SelectList(db.HoaDons, "ID", "Dia_chi_nhan", chitiethoadon.ID_Hoadon);
            return View(chitiethoadon);
        }

        //
        // GET: /Chitiethoadon/Delete/5

        public ActionResult Delete(int id = 0)
        {
            ChiTietHoaDon chitiethoadon = db.ChiTietHoaDons.Find(id);
            if (chitiethoadon == null)
            {
                return HttpNotFound();
            }
            return View(chitiethoadon);
        }

        //
        // POST: /Chitiethoadon/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            ChiTietHoaDon chitiethoadon = db.ChiTietHoaDons.Find(id);
            db.ChiTietHoaDons.Remove(chitiethoadon);
            db.SaveChanges();
            return RedirectToAction("Index");
        }
		[HttpPost]
        public ActionResult ajDelete(int id)
        {
             ChiTietHoaDon chitiethoadon = db.ChiTietHoaDons.Find(id);
			 if (chitiethoadon == null)
            {
                return Json(new { success = false ,msg="Không tìm thấy đối tượng"});
            }

         
            db.ChiTietHoaDons.Remove(chitiethoadon);
			db.SaveChanges();
            return Json(new { success=true});
        }
		
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}